/* This is a standalone program. Pass an image name as a first parameter of the program.
   Switch between standard and probabilistic Hough transform by changing "#if 1" to "#if 0" and back */
#include <cv.h>
#include <highgui.h>
#include <math.h>
#include <stdio.h>
#include <ctype.h> // pour isdigit()
#include "config.h"
#include "houghlines.h"

int mainHoughlines1(int argc, char** argv)
{
	IplImage* brgFrame;
    cvNamedWindow( "Source", 1 );
    cvNamedWindow( "Hough", 1 );
	if (argc != 1)
	{
    	CvCapture* camera = 0;
    	if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
    	{
        	camera = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
            printf("<mainHoughlines1> : caméra <%d>\n", argv[1][0] - '0');
    	}
    	else if( argc == 2 )
    	{
        	camera = cvCaptureFromFile( argv[1] );
            printf("<mainHoughlines1> : caméra <%s>\n", argv[1] );
    	}
    	if( camera )
    	{
    		IplImage* tmp;
			//Capturer une image
			cvGrabFrame( camera );
			//Aller chercher l’image sous format IplImage
			brgFrame = cvRetrieveFrame( camera );
			tmp = cvCloneImage(brgFrame);
	        fonctionhoughline(tmp);
			cvReleaseCapture( &camera	);
			//////cvReleaseImage( &brgFrame ); ////// Not release brgFrame !!!!!!!!!!!!!!!!
			cvReleaseImage( &tmp);
    	}
	}
	else
	{
    	printf("<mainHoughlines1> : pas de caméra\n");
		int numImage;
		for( numImage = 0; names[numImage] != 0; numImage++ )
	    {
			char nomImage[40] = "";
	        int touche;
			strcat(nomImage, folder);
			strcat(nomImage, names[numImage]);
	    	brgFrame		= cvLoadImage( nomImage, 0 );
	        if( !brgFrame )
	        {
	            printf("<mainHoughlines1> Couldn't load %s\n", names[numImage] );
	            continue;
	        }
    		printf("<mainHoughlines1> : image %d : %s chargée\n", numImage, names[numImage] );
	        fonctionhoughline(brgFrame);
	    	touche = cvWaitKey(0);
	        if( (char)touche == 27 )
	            break;
			cvReleaseImage( &brgFrame);
	    }
	}
	return 0;
}

int fonctionhoughline(IplImage* src)
{
    printf("<fonctionhoughline> Début\n");
    //const char* filename = argc >= 2 ? argv[1] : "pic1.png";
    //IplImage* src = cvLoadImage( filename, 0 );

    if( !(src!= 0) )
    {
    	printf("<fonctionhoughline> image NON\n");
        return -1;
    }
    else
    {
    	printf("<fonctionhoughline> image OK\n");
	        
	    IplImage* dst = cvCreateImage( cvGetSize(src), 8, 1 );
	    IplImage* color_dst = cvCreateImage( cvGetSize(src), 8, 3 );
	    CvMemStorage* storage = cvCreateMemStorage(0);
	    CvSeq* lines = 0;
	    int i;
	    cvCanny( src, dst, 50, 200, 3 );
	    cvCvtColor( dst, color_dst, CV_GRAY2BGR );
#if 1
        lines = cvHoughLines2( dst, storage, CV_HOUGH_STANDARD, 1, CV_PI/180, 100, 0, 0 );

        for( i = 0; i < MIN(lines->total,100); i++ )
        {
            float* line = (float*)cvGetSeqElem(lines,i);
            float rho = line[0];
            float theta = line[1];
            CvPoint pt1, pt2;
            double a = cos(theta), b = sin(theta);
            double x0 = a*rho, y0 = b*rho;
            pt1.x = cvRound(x0 + 1000*(-b));
            pt1.y = cvRound(y0 + 1000*(a));
            pt2.x = cvRound(x0 - 1000*(-b));
            pt2.y = cvRound(y0 - 1000*(a));
            cvLine( color_dst, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
        }
#else
        lines = cvHoughLines2( dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 50, 10 );
        for( i = 0; i < lines->total; i++ )
        {
            CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
            cvLine( color_dst, line[0], line[1], CV_RGB(255,0,0), 3, 8 );
        }
#endif
    }
    return 0;
}

int mainHoughlines2(int argc, char** argv)
{
    printf("<mainHoughlines2> Début de la fonction\n");
    IplImage* src;
	char nomImage[40] = "";
	strcat(nomImage, folder);
	strcat(nomImage, names[0]);
    if( (src=cvLoadImage(nomImage, 0))!= 0)
    {
    	printf("<mainHoughlines2> src ok\n");
        IplImage* dst = cvCreateImage( cvGetSize(src), 8, 1 );
        IplImage* color_dst = cvCreateImage( cvGetSize(src), 8, 3 );
        CvMemStorage* storage = cvCreateMemStorage(0);
        CvSeq* lines = 0;
        int i;
        cvCanny( src, dst, 50, 200, 3 );
        cvCvtColor( dst, color_dst, CV_GRAY2BGR );
#if 1
    	printf("<mainHoughlines2> CV_HOUGH_STANDARD\n");
        lines = cvHoughLines2( dst, storage, CV_HOUGH_STANDARD, 1, CV_PI/180, 100, 0, 0 );

        for( i = 0; i < MIN(lines->total,100); i++ )
        {
            float* line = (float*)cvGetSeqElem(lines,i);
            float rho = line[0];
            float theta = line[1];
            CvPoint pt1, pt2;
            double a = cos(theta), b = sin(theta);
            double x0 = a*rho, y0 = b*rho;
            pt1.x = cvRound(x0 + 1000*(-b));
            pt1.y = cvRound(y0 + 1000*(a));
            pt2.x = cvRound(x0 - 1000*(-b));
            pt2.y = cvRound(y0 - 1000*(a));
        	//cvLine( color_dst, pt1, pt2, CV_RGB(255,0,0), 3, CV_AA, 0 );
            //cvLine( color_dst, pt1, pt2, CV_RGB(255,0,0), 3, 8 );
            cvLine( color_dst, pt1, pt2, CV_RGB(255,0,0), 3, 8 , 0);
        }
#else
    	printf("<mainHoughlines2> CV_HOUGH_PROBABILISTIC\n");
        lines = cvHoughLines2( dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 50, 10 );
        for( i = 0; i < lines->total; i++ )
        {
            CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
            cvLine( color_dst, line[0], line[1], CV_RGB(255,0,0), 3, 8, 0 );
        }
#endif
        cvNamedWindow( "Source", 1 );
        cvShowImage( "Source", src );

        cvNamedWindow( "Hough", 1 );
        cvShowImage( "Hough", color_dst );

        cvWaitKey(0);
		cvReleaseImage( &src);
		cvReleaseImage( &dst);
		cvReleaseImage( &color_dst);
    }
	printf("<mainHoughlines2> Fin de la fonction\n");
    return 0;
}